const {Article} = require('../../model/article')
const formidable = require('formidable')
const path = require('path')

module.exports = (req,res,next) => {
    let id = req.query.id
    const form = formidable({
        uploadDir: path.join(__dirname,'../','../','public','upload'),
        keepExtensions:true
    });
    form.parse(req, async (err, fields, files) => {
        let option = {
            title: fields.title,
            author: fields.author,
            pubdate: fields.pubdate,
            content: fields.content
        }
        if(files.pic.size != 0){
            option.pic = path.join('../','static',files.pic.path.split('public')[1])
        }
       let result = await Article.updateOne({_id:id},option)
        res.redirect('/admin/article')
      });
}